app.controller('ConfigModuleCtrl', function ($scope, ajax, toaster) {

	$scope.reload = function () {
		ajax.call(new Req.GET({
			resource: 'config/module/bk'
		})).then(function (result) {
			$scope.list = result.result;
			for (var i = 0; i < $scope.list.length; i++) {
				var key = $scope.list[i].key;
				if (key == "booking.refurn.rule") {
					$scope.refundRule = $scope.list[i];//退票规则
					continue;
				} else if (key == "booking.order.inform") {
					$scope.order = $scope.list[i]; //订单需知
					continue;
				} else if (key == "booking.shift.timer") {
					$scope.shift = $scope.list[i]; //班次定时
					continue;
				} else if (key == "booking.terminal.timer") {
					$scope.teminal = $scope.list[i]; //终点定时
					continue;
				} else if (key == "booking.order.points") {
					$scope.points = $scope.list[i]; //积分配置
					continue;
				} else if (key == "booking.refund.timer") {
					$scope.timer = $scope.list[i]; //退票时间
					continue;
				} else if (key == "booking.insurance.inform") {
					$scope.ins = $scope.list[i];//保险说明
					continue;
				} else if (key == "booking.customer.protocol") {
					$scope.customer = $scope.list[i];//乘车协议
					continue;
				} else if (key == "booking.order.depart") {
					$scope.depart = $scope.list[i];//发车前关闭售票时间
					continue;
				} else if (key == "booking.ticket.no1") {
					$scope.ticketNo1 = $scope.list[i];// 票段2
					var ticket1 = $scope.ticketNo1.value;
					// var strs1 = new Array();
					var strs1 = ticket1.split(",");
					$scope.ticketOne1 = strs1[0];
					$scope.ticketOne2 = strs1[1];
					continue;
				} else if (key == "booking.ticket.no2") {
					$scope.ticketNo2 = $scope.list[i];// 票段2
					var ticket2 = $scope.ticketNo2.value;
					// var strs2 = new Array();
					var strs2 = ticket2.split(",");
					$scope.ticketTwo1 = strs2[0];
					$scope.ticketTwo2 = strs2[1];
					continue;
				} else if (key == "booking.ticket.now") {
					$scope.ticketNow = $scope.list[i]; //当前票号
				} else if (key == "booking.ticket.contacts") {
					$scope.contact = $scope.list[i]; //通知人
				} else if (key == "booking.insurance.no1") {
					$scope.insurNo1 = $scope.list[i];// 票段2
					var insur1 = $scope.insurNo1.value;
					var minmax1 = insur1.split(",");
					$scope.insurNo1Min = minmax1[0];
					$scope.insurNo1Max = minmax1[1];
					continue;
				} else if (key == "booking.insurance.no2") {
					$scope.insurNo2 = $scope.list[i];// 票段2
					var insur2 = $scope.insurNo2.value;
					var minmax2 = insur2.split(",");
					$scope.insurNo2Min = minmax2[0];
					$scope.insurNo2Max = minmax2[1];
					continue;
				} else if (key == "booking.insurance.now") {
					$scope.insurNow = $scope.list[i]; //当前票号
					continue;
				} else if (key == "booking.order.days") {
					$scope.days = $scope.list[i]; //预售天数
					continue;
				}
			}
		}, function () { });
	};

	var inserter = new Req.POST({ resource: $scope.resource });
	var updater = new Req.PUT({ resource: $scope.resource });
	var inrange = function (num1, num2, min, max) {
		return (Number(num1) >= Number(min) && Number(num1) <= Number(max))
			|| (Number(num2) >= Number(min) && Number(num2) <= Number(max));
	};

	$scope.reload();

	$scope.saveTicketOne = function (min, max) {
		if ($scope.ticketOneForm.$valid) {
			if (Number(min) > Number(max)) {
				toaster.pop('warning', '提示', '起始值必须小于最终值！');
				return;
			} else if (inrange(min, max, $scope.ticketTwo1, $scope.ticketTwo2)) {
				toaster.pop('warning', '提示', '主、备用号段不能有重合的号！');
				return;
			}
			$scope.ticketNo1.value = min + "," + max;
			$scope.save($scope.ticketNo1);
		}
	};

	$scope.saveTicketTwo = function (min, max) {
		if ($scope.ticketTwoForm.$valid) {
			if (Number(min) > Number(max)) {
				toaster.pop('warning', '提示', '起始值必须小于最终值！');
				return;
			} else if (inrange(min, max, $scope.ticketOne1, $scope.ticketOne2)) {
				toaster.pop('warning', '提示', '主、备用号段不能有重合的号！');
				return;
			}
			$scope.ticketNo2.value = min + "," + max;
			$scope.save($scope.ticketNo2);
		}
	};

	$scope.saveInsurOne = function (min, max) {
		if ($scope.insurNo1Form.$valid) {
			if (Number(min) > Number(max)) {
				toaster.pop('warning', '提示', '起始值必须小于最终值！');
				return;
			} else if (inrange(min, max, $scope.insurNo2Min, $scope.insurNo2Max)) {
				toaster.pop('warning', '提示', '主、备用号段不能有重合的号！');
				return;
			}
			$scope.insurNo1.value = min + "," + max;
			$scope.save($scope.insurNo1);
		}
	};
	$scope.saveInsurTwo = function (min, max) {
		if ($scope.insurNo2Form.$valid) {
			if (Number(min) > Number(max)) {
				toaster.pop('warning', '提示', '起始值必须小于最终值！');
				return;
			} else if (inrange(min, max, $scope.insurNo1Min, $scope.insurNo1Max)) {
				toaster.pop('warning', '提示', '主、备用号段不能有重合的号！');
				return;
			}
			$scope.insurNo2.value = min + "," + max;
			$scope.save($scope.insurNo2);
		}
	};

	$scope.fi = [];
	//班次筛选时段时间
	$scope.loadFilter = function () {
		var url = "booking.shift.filter";
		ajax.call(new Req.GET({
			resource: 'config/module/value?key=' + url
		})).then(function (result) {
			$scope.fi = result.result;
			var fi = $scope.fi.value;
			var strs = fi.split(",");
			$scope.total = [];
			for (var i = 0; i < strs.length; i++) {
				var sp = strs[i].split("@@");
				$scope.total.push(sp);
			}
		}, function () { });
	};
	$scope.loadFilter();

	$scope.fg = function (total) {
		if ($scope.fileForm.$valid) {
			var strs = total;
			var str = "";
			var flag = 0;
			for (var i = 0; i < strs.length; i++) {
				flag++;
				if (flag == strs.length) {
					str += strs[i][0] + '@@' + strs[i][1];
				} else {
					str += strs[i][0] + '@@' + strs[i][1] + ',';
				}
			}
			$scope.fi.value = str;
			updater.fields = $scope.fi;
			updater.id = $scope.fi.id;
			updater.resource = 'config/module/bk/update';
			ajax.call(updater).then(function (result) {
				if (result.code == 200) {
					toaster.pop('success', '成功', '修改信息成功。');
				} else {
					toaster.pop('error', '错误', '修改信息失败！');
				}
			}, function () {
				toaster.pop('error', '错误', '修改信息失败！');
			});
		} else {
			toaster.pop('warning', '提示', '填写格式错误或内容长度超出，请修改后再提交！');
			return;
		}
	};

	$scope.re = function () {
		var url = "booking.shift.filter";
		ajax.call(new Req.GET({
			resource: 'config/module/value?key=' + url
		})).then(function (result) {
			$scope.fi = result.result;
			var fi = $scope.fi.defaultValue;
			var strs = fi.split(",");
			$scope.total = [];
			for (var i = 0; i < strs.length; i++) {
				var sp = strs[i].split("@@");
				$scope.total.push(sp);
			}
			$scope.reset($scope.fi);
		}, function () { });
	};

	//保存修改
	$scope.save = function (module) {
		if (module.value == undefined) {
			toaster.pop('warning', '提示', '填写格式错误或内容长度超出，请修改后再提交！');
			return;
		}
		updater.fields = module;
		updater.id = module.id;
		updater.resource = 'config/module/bk/update';
		ajax.call(updater).then(function (result) {
			if (result.code == 200) {
				toaster.pop('success', '成功', '修改信息成功。');
				$scope.reload();
			} else {
				toaster.pop('error', '错误', '修改信息失败！');
			}
		}, function () {
			toaster.pop('error', '错误', '修改信息失败！');
		});
	};

	//恢复默认值
	$scope.reset = function (module) {
		inserter.resource = 'config/module/reset?key=' + module.key;
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				toaster.pop('success', '成功', '恢复默认值成功。');
				$scope.reload();
			} else {
				toaster.pop('error', '错误', '恢复默认失败！');
			}
		}, function () {
			toaster.pop('error', '错误', '恢复默认失败！');
		});
	};
});